package main

import (
	"path/filepath"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/storage"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)
func ListSlice(ss []string) *widget.List{
	return widget.NewList(func() int {
		return len(ss)
	},func() fyne.CanvasObject {
		return widget.NewLabel("aaaaa")
	},func(lii widget.ListItemID, co fyne.CanvasObject) {
		lbl:=co.(*widget.Label)
		lbl.SetText(ss[lii])
	})
}
func Table(data [][]string) *widget.Table {
	tbl1 := widget.NewTable(nil, nil, nil)
	tbl1.Length = func() (int, int) {
		// 处理首行是合并单元格的情况
		maxCol := 0
		for _, v := range data {
			maxCol = Max(maxCol, len(v))
		}
		return len(data), maxCol
		// return len(data), len(data[0])
	}
	tbl1.CreateCell = func() fyne.CanvasObject {
		return widget.NewLabel("table")
	}
	tbl1.UpdateCell = func(id widget.TableCellID, template fyne.CanvasObject) {
		lbl := template.(*widget.Label)
		// data1, err := f.GetRows("Sheet1"),为避免合并单元格造成数组越界
		// 如果数据为data1, err := f.GetCols("Sheet1")，则需要对id.Row进行越界检查
		if id.Col < len(data[id.Row]) {
			lbl.SetText(data[id.Row][id.Col])
		} else {
			lbl.SetText("")
		}
	}
	return tbl1
}
func LblList(dataList binding.StringList) *widget.List {
	return widget.NewListWithData(dataList,
		func() fyne.CanvasObject {
			return widget.NewLabel("aaaaaaaaaaaaaaaaaaaaaaaaaa")
		}, func(di binding.DataItem, co fyne.CanvasObject) {
			lbl := co.(*widget.Label)
			s := di.(binding.String)
			ss, _ := s.Get()
			lbl.SetText(filepath.Base(ss))
		})
}

type RightClickLbl struct {
	widget.Label
}

func (i *RightClickLbl) Tapped(_ *fyne.PointEvent) {
	bls, _ := bl.Get()
	for _, v := range bls {
		if filepath.Base(v) == i.Text {
			ui.setDirectory(storage.NewFileURI(v))
			return
		}
	}
}
func (i *RightClickLbl) TappedSecondary(e *fyne.PointEvent) {
	m := fyne.NewMenu("file",
		fyne.NewMenuItem("Delete", func() {
			bls, _ := bl.Get()
			for _, v := range bls {
				if filepath.Base(v) == i.Text {
					bls = Deletes(bls, v)
					bl.Set(bls)
					msg.Set("Delete " + i.Text)
					return
				}
			}
		}))
	widget.ShowPopUpMenuAtPosition(m,
		fyne.CurrentApp().Driver().CanvasForObject(i), e.AbsolutePosition)
}
func NewRightClickLbl(txt string) *RightClickLbl {
	lbl := &RightClickLbl{}
	lbl.SetText(txt)
	return lbl
}
func RightClickLblList(dataList binding.StringList) *widget.List {
	return widget.NewListWithData(dataList,
		func() fyne.CanvasObject {
			return NewRightClickLbl("aaaaaaaaaaaaaaaaaaaaaaaaaa")
		}, func(di binding.DataItem, co fyne.CanvasObject) {
			lbl := co.(*RightClickLbl)
			s := di.(binding.String)
			ss, _ := s.Get()
			lbl.SetText(filepath.Base(ss))
		})
}

func DeleteList(dataList binding.StringList) *widget.List {
	return widget.NewListWithData(dataList,
		func() fyne.CanvasObject {
			return container.NewHBox(widget.NewLabel(""),layout.NewSpacer(),
		widget.NewButtonWithIcon("",theme.DeleteIcon(),nil))
		}, func(di binding.DataItem, co fyne.CanvasObject) {
			s := di.(binding.String)
			ss, _ := s.Get()
			hb:=co.(*fyne.Container)
			lbl := hb.Objects[0].(*widget.Label)
			btn := hb.Objects[2].(*widget.Button)
			btn.OnTapped=func() {
				bls, _ := bl.Get()
					bls = Deletes(bls, ss)
					bl.Set(bls)
					msg.Set("Delete " + ss)
			}
			lbl.SetText(filepath.Base(ss))

		})
}

func BtnLblList(dataList binding.StringList) *widget.List {
	return widget.NewListWithData(dataList,
		func() fyne.CanvasObject {
			return container.NewHBox(
				widget.NewButton("-", nil),
				widget.NewLabel("aaaaa"),
			)
		}, func(di binding.DataItem, co fyne.CanvasObject) {
			hbox := co.(*fyne.Container)
			btn := hbox.Objects[0].(*widget.Button)
			lbl := hbox.Objects[1].(*widget.Label)
			s := di.(binding.String)
			ss, _ := s.Get()
			lbl.SetText(ss)

			btn.OnTapped = func() {
				data, _ := dataList.Get()
				dataList.Set(Deletes(data, ss))
			}
		})
}

// func NewOpenFile(data binding.String, w fyne.Window) *fyne.Container {
// 	return container.NewBorder(nil, nil, widget.NewButtonWithIcon("", theme.FileIcon(), func() {
// 		d := dialog.NewFileOpen(func(uc fyne.URIReadCloser, e error) {
// 			if uc == nil {
// 				return
// 			}
// 			data.Set(uc.URI().String()[7:])
// 		}, w)
// 		luri, _ := storage.ListerForURI(storage.NewFileURI("."))
// 		d.SetFilter(storage.NewExtensionFileFilter([]string{".xlsx"}))
// 		d.SetLocation(luri)
// 		d.Show()
// 	}),
// 		widget.NewButtonWithIcon("", theme.FileTextIcon(), func() {
// 			w := fyne.CurrentApp().NewWindow("data browse")
// 			f1path, _ := data.Get()
// 			f, err := excelize.OpenFile(f1path)
// 			if err != nil {
// 				return
// 			}
// 			data1, err := f.GetRows("Sheet1")
// 			if err != nil {
// 				return
// 			}
// 			tbl := Table(data1)
// 			w.SetContent(tbl)
// 			w.Resize(fyne.NewSize(600, 600))
// 			w.Show()
// 		}), widget.NewEntryWithData(data))
// }

// outData := binding.NewString()
// out := NewOpenFolder(outData, w)
// outData1 := binding.NewString()
// out1 := NewOpenFolder(outData1, w)
func NewOpenFolder(data binding.String, w fyne.Window) *fyne.Container {
	return container.NewBorder(nil, nil, widget.NewButtonWithIcon("", theme.FolderOpenIcon(), func() {
		d := dialog.NewFolderOpen(func(lu fyne.ListableURI, e error) {
			if lu == nil {
				return
			}
			data.Set(lu.Path())
		}, w)
		luri, _ := storage.ListerForURI(storage.NewFileURI("."))
		d.SetLocation(luri)
		d.Show()
	}), nil, widget.NewEntryWithData(data))
}
